package com.gitee.linmt.service;

import com.gitee.linmt.entity.Location;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * LocationService 实现类.
 *
 * @author <a href="mailto:lin-mt@outlook.com">lin-mt</a>
 */
@Service
public class LocationServiceImpl {
    
    private static final Map<String, Set<Location>> USERNAME_TO_LOCATIONS;
    
    static {
        USERNAME_TO_LOCATIONS = new HashMap<>(2);
        Set<Location> locations = new HashSet<>();
        locations.add(new Location("寺庙"));
        USERNAME_TO_LOCATIONS.put("大秃驴", locations);
        USERNAME_TO_LOCATIONS.put("小和尚", locations);
        locations = new HashSet<>();
        locations.add(new Location("尼姑庵"));
        USERNAME_TO_LOCATIONS.put("尼姑", locations);
    }
    
    /**
     * 验证用户名为 username 的用户是否有权限访问 location.
     *
     * @param username 用户名
     * @param location 地址
     * @return 如果没有权限返回 false，有权限返回 true
     */
    public boolean hasPermission(final String username, final String location) {
        return USERNAME_TO_LOCATIONS.get(username).contains(new Location(location));
    }
}
